@extends('admin.adminbase')
@section('addcss')
    <style>
        input{
            border: 1px solid #ccc;
            border-radius: 0.3em;
            display: inline-block;
            padding:2px;
        }
    </style>

@endsection
@section('content')
    <section class="hbox stretch">
        @include("admin.public.left")
        <aside>
            <section class="vbox">
                <header class="header bg-white b-b clearfix">
                    <form class="talbe-search" method="get" action="{{url('admin/article')}}">
                        <div class="row m-t-sm">
                            <div class="col-sm-2 m-b-xs">
								<a role="button" class="btn btn-sm btn-primary" title="添加资讯" href="{{url("admin/article/create")}}">添加资讯
									<i class="glyphicon glyphicon-plus"></i>
								</a>
							</div>

                            <div class="col-sm-9" style="padding-top: 5px;">
                                <label>标题名</label>
                                <input type="text" name="title" value="{{ old('title') }}" style="margin-right: 5px;">

                                <label>开始时间</label>
                                <input type="text" name="start_time" value="{{ old('start_time') }}" id="start">
                                -
                                <label>结束日期</label>
                                <input type="text" name="end_time" value="{{ old('end_time') }}" id="end" style="margin-right: 5px;">

                                <select class="input-sm"  name="type">
                                    <option value="0">请选择</option>
                                    @foreach($typeList as $type)
                                    <option value="{{ $type->id }}" @if($type->id == old('type')) selected="selected" @endif>{{ $type->name }}</option>
                                    @endforeach
                                </select>
                            </div>

                            {!! csrf_field() !!}
                            <button class="btn btn-sm btn-default" type="submit" title="搜索">
                                <i class="glyphicon glyphicon-search"></i> 搜索
                            </button>
                        </div>
                    </form>
                </header>

                <section class="scrollable  wrapper w-f">
                    <section class="panel panel-default">
                        <div class="table-responsive">
                            <table class="table table-hover m-b-none entity-view" data-path="/attendance/count/countDetail/id/{0}">
                                <thead>
                                <tr>
                                    <th style="text-align: center;">序号</th>
                                    <th style="text-align: center;">标题</th>
                                    <th style="text-align: center;">作者</th>
                                    <th style="text-align: center;">类别</th>
                                    <th style="text-align: center;" class="status">状态</th>
                                    <th style="text-align: center;">发布时间</th>
                                    <th style="text-align: center;">操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                    @foreach ($res as $k=>$v)
                                    <tr>
                                        <td style="text-align: center;">{{$limit*($res->currentPage()-1)+$k+1}}</td>
                                        <td style="text-align: center;">{{ $v->title }}</td>
                                        <td style="text-align: center;">{{ $v->author }}</td>
                                        <td style="text-align: center;">
                                            @foreach($typeList as $type)
                                                @if($type->id == $v->type_id)
                                                    {{ $type->name }}
                                                @endif
                                            @endforeach
                                        </td>
                                        <td style="text-align: center;">{{ \App\Http\Util\Status::getLabel($v->status) }}</td>
                                        <td style="text-align: center;">{{ \App\Http\Util\TimeHelper::formatDate($v->created_at) }}</td>
                                        <td style="text-align: center">
                                            <a href="{{ url('admin/article/edit/'.$v->id) }}" class="label label-success">编辑</a>
                                            <a data-back="{{ url('admin/article/delete/'.$v->id)}}"  class="del label label-danger">删除</a>
                                            <a data-id="{{$v->id}}" data-able="{{$v->status}}" class="label label-info able">@if($v->status == 1)禁用@else启用@endif</a>
                                        </td>
                                    </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    </section>
                    <div style="text-align: center">{!! $res->appends($_GET)->render() !!}</div>
                </section>

            </section>
        </aside>
    </section>

@endsection

@section('addscript')
    <script src="{{ url('/') }}/laydate/laydate.js"></script>
    <script>
        var start = {
            elem: '#start',
            format: 'YYYY-MM-DD',
            max: '2099-06-16', //最大日期
            istime: false,
            istoday: true,
            choose: function(datas){
                end.min = datas; //开始日选好后，重置结束日的最小日期
                end.start = datas //将结束日的初始值设定为开始日
            }
        };
        var end = {
            elem: '#end',
            format: 'YYYY-MM-DD',
            max: '2099-06-16',
            istime: false,
            istoday: true,
            choose: function(datas){
                start.max = datas; //结束日选好后，重置开始日的最大日期
            }
        };
        laydate(start);
        laydate(end);

		$(function(){
            $(".del").click(function(){
                var url=$(this).attr("data-back");

                layer.confirm('您确定删除吗？', {
                    btn: ['确认','取消'] //按钮
                }, function(){
                    $.post(url,{},function(data){
                        if(data){
                            layer.alert("删除成功！",function(){
                                location.reload();
                            });
                        }else{
                            layer.alert(data.msg)
                        }
                    });
                }, function(){

                })
            })
            $(".able").click(function(){
                var id=$(this).data("id");
                var able = $(this).data("able");
                if(able==1) {
                    able=2;
                } else {
                    able=1;
                }
                var mythis = $(this);
                $.ajax({
                    'url':"/admin/article/able/"+id+"/"+able,
                    'type':"get",
                    'dataType':'json',
                    'success':function(data){
                        if(data.success){
                            mythis.parent().find('.able').text(data.msg);
                            mythis.parent().find('.able').data('able',data.data);
                            if (data.data==1) {
                                var m ="启用"
                            } else {
                                var m ="禁用"
                            }
                            mythis.parent().parent().find('.status').text(m);
                        }else{
                            layer.alert(data.msg)
                        }
                    }
                })
            })
		});
	</script>
@endsection
